const Results = require('../../config/results')
const noneFolderCreateFolderByUpLoad = require("../system/handleFiles");
const multer = require("multer");
const upload = multer({
    storage: multer.diskStorage({
        destination: (req, file, cb) => cb(null, noneFolderCreateFolderByUpLoad(req, file).absoluteDirPath),
        filename: (req, file, cb) => cb(null, noneFolderCreateFolderByUpLoad(req, file).fileNamePath)
    }),
    fileFilter: (req, file, cb) => {
        cb(null, true)
    }
})
module.exports = {
    handleMulterConf: upload,
    handleUpload: (req, res, next) => {
        let host = '';
        req.rawHeaders.forEach((item, index) => item === 'Host' ? host = req.rawHeaders[index + 1] : void 0)

        let target = {
            host,
            url: req.protocol + "://" + host + noneFolderCreateFolderByUpLoad(req, req.app.file).realtiveDirPath + noneFolderCreateFolderByUpLoad(req, req.app.file).fileNamePath,
            ...noneFolderCreateFolderByUpLoad(req, req.app.file)
        }
        return res.status(200).json(Results.success(200, 'ok', target))
    }
}
